<?php
namespace app\admin666\controller;

use think\Request;
use think\cache;
class System extends Admin
{
    // 获取某个标签的配置参数
    public function group() {
        $id     =   input('get.id',1);
        $type   =   config('CONFIG_GROUP_LIST');
        $list   =   model("Config")->field('id,name,title,extra,value,remark,type,group')
        ->where(['display'=>['gt',0]])->order('sort')->select();
        
        $alist = [];
        foreach ($type as $key=>$val){
            foreach ($list as $k=>$v){
                if($key == $v['group']){
                    $alist[$key][] = $v;
                }
            }
        }
        
        $this->assign('list',$alist);
		return $this->fetch();
    }
	
	//添加变量配置
	public function add(){
		if(Request::instance()->isPost()){
			$m = model('Config');
			$data = $m->create();
			if($data['type'] == 4){
				$data['extra'] = $data['value'];	
				$data['value'] = 0;
			}
			$data['display'] = 1; 
			if($m->add($data)){
			    cache('DB_CONFIG_DATA',null);
			    $this->success('添加成功！',url('System/group'));	
			}else{
				$this->error('添加失败！');	
			}	
		}
		else{
			$this->display();	
		}	
	}
	
	
	/**
     * 批量保存配置
     */
    public function save($config){
        if($config && is_array($config)){
            $Config = model('Config');
            foreach ($config as $name => $value) {
                $map = ['name' => $name];
                $Config->where($map)->setField('value', $value);
            }
        }
        cache('DB_CONFIG_DATA',null);
        $this->success('保存成功！');
    }

    /**
    * 清除缓存
    * @date: 2016年8月6日 下午3:44:49
    * @author: xiaocui
    * @param: variable
    * @return:
    */
    public function cache(){
        if(IS_POST){
            Cache::clear();
            $this->success('缓存清除成功！');
        } else {
            $this->meta_title = '清除缓存';
            $this->display();
        }
    }
    
}
